<?php
namespace app\index\controller;

use think\Controller;
use think\Session;
use think\Paginator;
use think\Cache;
use think\DB;

class Personalcenter extends BaseController{

	public function index(){
		$this->checkIndex();
		$usename = Session::get('userinfo');
		$this->assign('uname',$usename);
		return view();
	}

	/**
	 * 修改密码
	 */
	public function editpassword(){
		$this->checkIndex();
		return view();
	}

	/**
	 * 执行修改密码
	 */
	public function doeditpassword(){
		$data = input('post.');
		 // return $data;
		if ($data) {
			$datas['oldpass'] = md5($data['oldpass']);
			$datas['newpass'] = md5($data['password']);
		}
		$usename = Session::get('account');
		// return $datas['oldpass'];
		$oldpass = db('user')->where('account',$usename)->find();
		// return $oldpass['pass'];
		if ($oldpass) {
			if ($oldpass['pass'] == $datas['oldpass']) {
				$result = db('user')->where('account',$usename)->setField('pass',$datas['newpass']);
				if ($result==1) {
					session_destroy();
					return $result;
				}else{
					return array('msg'=>'修改失败','status'=>'2');
				}
			}else{
				return array('msg'=>'原始密码输入错误','status'=>'3');
			}
		}else{
			return array('msg'=>'没有指定用户','status'=>'4');
		}
	}

	/**
	 * 在读书籍--归还申请
	 */
	public function applyrollback(){
		$bid = input('post.');
		//当前登录的用户
		$userid = Session::get('userid');
		// return $bid;
		$result = db('borrow')->where('uid',$userid)->where('states=1 or states=5')->where('bid',$bid['id'])->setField('states',6);
		// dump($result);die;
		return $result;
	}

	/**
	 * 再次申请借阅
	 */
	public function againapplyread(){
		$bid = input('post.');
		//当前登录的用户
		$userid = Session::get('userid');
		// return $bid;
		$result = db('borrow')->where('uid',$userid)->where('states',2)->where('bid',$bid['id'])->setField(array('states'=>3,'applybooktime'=>time()));
		// dump($result);die;
		return $result;
	}
	/**
	 * 预约书籍--取消预约
	 */
	public function cancelapply(){
		
		$bid = input('post.');
		//当前登录的用户
		$userid = Session::get('userid');
		//更改数据库借阅记录的状态---改为取消预约
		$result = db('borrow')->where('uid',$userid)->where('states',8)->where('bid',$bid['id'])->setField('states',4);
		return $result;
	}


	/**
     * 取消申请
     */
    public function cancelapplys(){
        $bid = input('post.');
        // dump($bid);die;
        //当前登录的用户
        $userid = Session::get('userid');
        //更改数据库借阅记录的状态---改为取消预约
        $result = db('borrow')->where('uid',$userid)->where('states',3)->where('bid',$bid['id'])->setField('states',4);
        return $result;
    }






	/**
     * 分页-在读
     */
    public function pageBookRecord(){
        $page = $_POST['page'];
        if(empty($page)){
            $page =1;
        }

        $book = db('books')->where('bstates',1)->select();
        // dump($book);die;

        $userid = Session::get('userid');
		//书籍表--已借出的书籍
		// $book = db('books')->where('bstates',2)->select();
		//关联的借阅表--在读书籍
		$borrowstates = db('borrow')->where('states=1 or states=5 or states=6')->where('uid',$userid)->select();
		// 在读书籍
		$dataarray = array();
		$m = 0;
		foreach ($borrowstates as $k => $v) {
			$overtime = (int)ceil((time()-$borrowstates[$k]['expectedReturnTime'])/3600/24);
			$borrowstates[$k]['booktime'] = date('Y/m/d',$borrowstates[$k]['booktime']);
			$borrowstates[$k]['expectedReturnTime'] = date('Y/m/d',$borrowstates[$k]['expectedReturnTime']);
			//当前登录用户的在读书籍
			$book = db('books')->where('bstates',2)->where('id',$v['bid'])->select();
			// dump($book);die;
			$dataarray[$m]['name'] = $book[0]['bname'];
			$dataarray[$m]['id'] = $book[0]['id'];
			$dataarray[$m]['img'] = $book[0]['img'];
			$dataarray[$m]['states'] = $borrowstates[$k]['states'];
			$dataarray[$m]['booktime'] = $borrowstates[$k]['booktime'];
			$dataarray[$m]['expectedReturnTime'] = $borrowstates[$k]['expectedReturnTime'];
			$dataarray[$m]['overtime'] = $overtime;
			$m++;
		}

        $query = Publicmethod::pageing($dataarray,$page);
        
        if($query){
            return $query;
        }
        return 0;
    }

    /**
     * 分页-预约或借阅申请
     */
    public function pageBookcancel(){
    	
    	$page = $_POST['page'];
        if(empty($page)){
            $page =1;
        }
        $userid = Session::get('userid');
        $borrowapply = db('borrow')->where('states=8 or states=3 or states=2')->where('uid',$userid)->select();
		$dataapply = array();
		$n = 0;
		foreach ($borrowapply as $k => $v) {
			$borrowapply[$k]['applybooktime'] = date('Y/m/d',$borrowapply[$k]['applybooktime']);
			//当前登录用户的预约书籍
			$book = db('books')->where('isdelete',1)->where('id',$v['bid'])->select();
			if ($book) {
				$dataapply[$n]['name'] = $book[0]['bname'];
				$dataapply[$n]['id'] = $book[0]['id'];
				$dataapply[$n]['img'] = $book[0]['img'];
				$dataapply[$n]['states'] = $v['states'];
				$dataapply[$n]['applybooktime'] = $borrowapply[$k]['applybooktime'];
				$n++;
			}
		}
		$query = Publicmethod::pageing($dataapply,$page);
        
        if($query){
            return $query;
        }
        return 0;
    }

    /**
     * 分页-记录
     */
    public function pageBooklist(){
    	
    	$page = $_POST['page'];
        if(empty($page)){
            $page =1;
        }
        $userid = Session::get('userid');
        //借阅记录
		$applyrecord = db('borrow')->where('uid',$userid)->where('states=1 or states=5 or states=6 or states=7')->select();
		$datarecord = array();
		$s = 0;
		foreach ($applyrecord as $k => $v) {
			$overtime = (int)ceil(($applyrecord[$k]['returntime']-$applyrecord[$k]['expectedReturnTime'])/3600/24);
			$applyrecord[$k]['booktime'] = date('Y/m/d',$applyrecord[$k]['booktime']);
			$applyrecord[$k]['expectedReturnTime'] = date('Y/m/d',$applyrecord[$k]['expectedReturnTime']);
			$applyrecord[$k]['returntime'] = date('Y/m/d',$applyrecord[$k]['returntime']);
			//当前登录用户的预约书籍
			$book = db('books')->where('id',$v['bid'])->select();
			$datarecord[$s]['name'] = $book[0]['bname'];
			$datarecord[$s]['img'] = $book[0]['img'];
			$datarecord[$s]['id'] = $book[0]['id'];
			// $datarecord[$s]['states'] = $book[0]['states'];
			$datarecord[$s]['overtime'] = $overtime;
			$datarecord[$s]['booktime'] = $applyrecord[$k]['booktime'];
			$datarecord[$s]['expectedReturnTime'] = $applyrecord[$k]['expectedReturnTime'];
			$datarecord[$s]['returntime'] = $applyrecord[$k]['returntime'];
			$s++;
		}
		$query = Publicmethod::pageing($datarecord,$page);
        
        if($query){
            return $query;
        }
        return 0;
    }

   
}
